package ru.pomarshrutu.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;

public class UploadRecordingWidget extends Composite {

	private UploadRecordingWidgetListener listener;
	private Label coordinatesLabel;
	private FileUpload fileUpload;
	private Button removeButton;
	private VerticalPanel panel;
	private Button uploadButton;
	private FormPanel form;
	private Image loaderImage;
	private Image tickImage;
	private Image crossImage;
	
	public enum State { NEW, SUBMITTED_OK, SUBMITTED_ERROR }
	private State state = State.NEW;
	private LatLng coordinates;
	private Marker marker;

	public UploadRecordingWidget(UploadRecordingWidgetListener listener) {
		this.listener = listener;
		init();
	}
	
	private void init() {
		loaderImage = new Image("ajax-loader.gif");
		tickImage = new Image("tick.png");
		crossImage = new Image("cross.png");
		
		form = new FormPanel();
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.setAction(GWT.getModuleBaseURL() + "simpleupload");
		
		panel = new VerticalPanel();
		
		HorizontalPanel line1 = new HorizontalPanel();
		panel.add(line1);
		
		line1.add(new Label("Координаты:"));
		
		coordinatesLabel = new Label();
		line1.add(coordinatesLabel);
		
		HorizontalPanel line2 = new HorizontalPanel();
		panel.add(line2);
		
		line2.add(new Label("Файл:"));
		
		fileUpload = new FileUpload();
		fileUpload.setName("fileUpload");
		line2.add(fileUpload);
		
		uploadButton = new Button("Загрузить");
		uploadButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				listener.onUpload(UploadRecordingWidget.this);
			}
		});
		line2.add(uploadButton);
		
		form.add(panel);
		
		form.addSubmitHandler(new FormPanel.SubmitHandler() {
			@Override
			public void onSubmit(SubmitEvent event) {
				loaderImage.setVisible(true);
				fileUpload.setEnabled(false);
				uploadButton.setEnabled(false);
				removeButton.setEnabled(false);
			}
		});
		form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				loaderImage.setVisible(false);
				String results = event.getResults();
				if(results.length() == 0 || "<pre></pre>".equals(results)) { //submit was successfull
					removeButton.setEnabled(true);
					tickImage.setVisible(true);
					state = State.SUBMITTED_OK;
				}
				else { //submit failed
					crossImage.setVisible(true);
					crossImage.setTitle(results);
					state = State.SUBMITTED_ERROR;
				}
			}
		});
		
		removeButton = new Button("Удалить");
		removeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				listener.onRemove(UploadRecordingWidget.this);
			}
		});
		line2.add(removeButton);
		
		line2.add(loaderImage);
		loaderImage.setVisible(false);
		
		line2.add(tickImage);
		tickImage.setVisible(false);
		
		line2.add(crossImage);
		crossImage.setVisible(false);
		
		initWidget(form);
	}
	
	public void setCoordinates(LatLng coordinates) {
		this.coordinates = coordinates;
		coordinatesLabel.setText(coordinates.toUrlValue());
	}
	
	public LatLng getCoordinates() {
		return coordinates;
	}
	
	public String getFileName() {
		return fileUpload.getFilename();
	}
	
	public void setMapMarker(Marker marker) {
		this.marker = marker;
	}
	
	public Marker getMapMarker() {
		return marker;
	}
	
	public State getState() {
		return state;
	}
	
	public void submit() {
		form.setAction(GWT.getModuleBaseURL() + "simpleupload?action=addPoint&coordinates=" + coordinates.toUrlValue());
		form.submit();
	}
	
}
